package com.yunhe.merchants.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ModelNotFoundException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.utils.ExcelUtils;
import com.yunhe.merchants.domain.MyProject;
import com.yunhe.merchants.domain.ProcessStatus;
import com.yunhe.merchants.model.FilterCondition;
import com.yunhe.merchants.model.IntentionProjectUpload;
import com.yunhe.merchants.model.TalkUpload;
import com.yunhe.merchants.service.IDataHistoryProjectStatusService;
import com.yunhe.merchants.service.IProcessStatusService;
import com.yunhe.merchants.service.ITalkService;
import com.yunhe.merchants.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: DongWenYu
 * @Date: 2021/5/7 22:18
 */
@Api(value = "talk", description = "洽谈中项目", tags = {"Model_Talk_Project"})
@RestController
@RequestMapping("/talk")
public class TalkController {

    @Autowired
    ITalkService talkService;

    @Autowired
    IProcessStatusService processStatusService;

    @Autowired
    IDataHistoryProjectStatusService dataHistoryProjectStatusService;

    @Autowired
    MyHeaderUtil headerUtil;

    @ApiOperation("洽谈中条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping
    public ResponseEntity<ResultObject> selectByCondition(@ModelAttribute FilterCondition filterCondition,
                                                                   @RequestParam("page") int page,
                                                                   @RequestParam("size") int size) throws ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        PageResult result = talkService.selectByFilterCondition(filterCondition,userId,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("数据导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "意向项目的id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/export")
    public ResponseEntity<ResultObject> export(@RequestParam("ids") String ids,
                                               @ModelAttribute FilterCondition filterCondition,
                                               HttpServletResponse httpServletResponse) {
        List<MyProject> data = talkService.export(ids,filterCondition);
        ExcelUtils.convertAndExport(data , httpServletResponse , TalkUpload.class);
        return null;
    }

    @ApiOperation("查看洽谈中项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "洽谈中项目id", dataType = "long", paramType = "query")
    })
    @GetMapping("/detail")
    public ResponseEntity<ResultObject> selectById(@RequestParam("id") Long id){
        MyProject result = talkService.selectById(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("查看洽谈的的历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "意向项目id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页展示的条数", dataType = "int", paramType = "query")
    })
    @GetMapping("/history")
    public ResponseEntity<ResultObject> selectHistory(@RequestParam("id") Long id,
                                                               @RequestParam("page") int page,
                                                               @RequestParam("size") int size){
        PageResult result = dataHistoryProjectStatusService.selectByProjectId(id,page ,size);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("查看洽谈的状态流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String", paramType = "query")
    })
    @GetMapping("/status")
    public ResponseEntity<ResultObject> selectStatus(@RequestParam("id") Long id,
                                                     @RequestParam(value = "status", required = false) String status){
        ProcessStatus result = talkService.selectStatusById(id ,status);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("更新意向项目的状态流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "意向项目id", dataType = "long", paramType = "query")
    })
    @PostMapping("/status")
    public ResponseEntity<ResultObject> updateProcessStatus(@RequestBody ProcessStatus processStatus,
                                                            @RequestParam("id") Long id) throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException, ModelNotFoundException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        processStatusService.update(processStatus , id , userId);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("洽谈项目状态下一步")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "意向项目id", dataType = "long", paramType = "query")
    })
    @PostMapping("/next")
    public ResponseEntity<ResultObject> next(@RequestParam("id") Long id) throws ArgumentErrorException {
        Long userId = headerUtil.getHeaderUser();
        if(userId == null){
            throw new ArgumentErrorException("未获取到登录信息，请重新登录！");
        }
        processStatusService.next(id , userId);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }
}
